package com.easyUploader.entity.vo;

import com.common.util.MybatisUtil;
import com.common.util.StringUtil;
import com.easyUploader.service.impl.BucketServiceImpl;
import com.easyUploader.threadHolder.UserThreadHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

import static com.easyUploader.property.LocalFileProperty.DEFAULT_BUCKET;

/**
 * @author 大忽悠
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@Slf4j
@ApiModel(description = "存储桶")
public class BucketVo {
    /**
     * 存储桶名称
     */
    @ApiModelProperty("存储桶名称")
    private String name;

    /**
     * 存储桶类型: 0-只读,1-读写
     */
    @ApiModelProperty("存储桶类型: 0-只读,1-读写")
    private Integer bucketType;

    /**
     * 访问类型: 0-私有,1-公开
     */
    @ApiModelProperty("访问类型: 0-私有,1-公开")
    private Integer accessType;

    public static String getDefaultIfNeeded(String bucket) {
            return StringUtil.isNullOrEmpty(bucket)?DEFAULT_BUCKET:bucket;
    }

    public void safeCheck(BucketServiceImpl bucketService) {
        if (StringUtil.isNullOrEmpty(name)) {
            throw new IllegalArgumentException("存储桶名称为空");
        }
        //校验存储桶名称是否重复
        MybatisUtil.repeatCheck(bucketService,Map.of("name",name,"create_by",UserThreadHolder.get().getId()),"存储桶名称重复");
        this.bucketType = (bucketType==null || bucketType>0)?1:0;
        this.accessType = (accessType==null || accessType>0)?1:0;
    }
}
